package com.zhiyume.controller;

import com.zhiyume.dao.entity.TPerson;
import com.zhiyume.dao.entity.TPersonWithBLOBs;
import com.zhiyume.service.TPersonService;
import com.zhiyume.service.common.ConstantEnum;
import com.zhiyume.service.util.JsonUtil;
import com.zhiyume.service.util.StringUtil;
import com.zhiyume.util.TypeConstants;
import com.zhiyume.vo.PersonReqVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/person")
public class PersonController {
    @Autowired
    private TPersonService tPersonService;

    /**
     * 根据Id查询人员信息
     * @return
     */
    @RequestMapping("/findById")
    public String findPersonById(long id){
       if(id==0){
           return JsonUtil.returnErrorInfo(ConstantEnum.CONSTANT_MISS_PARA.getErrType(),ConstantEnum.CONSTANT_MISS_PARA.getErrMsg());
       }
       return JsonUtil.returnSuccessInfo(ConstantEnum.CONSTANT_SUCCESS.getErrType(),ConstantEnum.CONSTANT_SUCCESS.getErrMsg(),tPersonService.findPersonById(id));
    }
    /**
     * 根据name查询人员信息
     * @return
     */
    @RequestMapping("/findByName")
    public String findPersonByName(String name){
        if(StringUtil.isBlank(name)){
            return JsonUtil.returnErrorInfo(ConstantEnum.CONSTANT_MISS_PARA.getErrType(),ConstantEnum.CONSTANT_MISS_PARA.getErrMsg());
        }
        return tPersonService.findPersonByName(name);
    }

    /**
     * 添加人员信息
     * @param tPerson
     * @return
     */
    @RequestMapping("/addPerson")
    public String addPerson(TPersonWithBLOBs tPerson) {
        if(null == tPerson || StringUtil.isBlank(tPerson.getName())) {
            return JsonUtil.returnErrorInfo(ConstantEnum.CONSTANT_MISS_PARA.getErrType(),ConstantEnum.CONSTANT_MISS_PARA.getErrMsg());
        }
        return tPersonService.addPerson(tPerson);
    }

    /**
     * 添加人员信息
     * @return
     */
    @RequestMapping("/actorList")
    public ModelAndView actorList(){
        // 栏目下拉列表
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/person/actorList");
        return modelAndView;
    }

    /**
     * 使用自定义mapper查询明星列表
     * @return
     */
    @RequestMapping("/getAllActorDataList")
    @ResponseBody
    public Map<String,Object> getAllActorDataList(PersonReqVO reqVO){
        // 栏目下拉列表
        Map<String,Object> dataMap = new HashMap();
        List<TPersonWithBLOBs> list = tPersonService.queryActorList(reqVO);
        list = list.size() > 0 ? list : null;
        int count = tPersonService.countActor(reqVO);
        dataMap.put("list",list);
        dataMap.put("total",count);
        dataMap.put("page",reqVO.getPage());
        return dataMap;
    }

    /**
     * 列表过滤查询
     * @return
     */
    @RequestMapping("/searchActorList")
    @ResponseBody
    public Map<String,Object> searchActorList(PersonReqVO reqVO){
        // 栏目下拉列表
        Map<String,Object> dataMap = new HashMap();
        List<TPersonWithBLOBs> list = tPersonService.searchActorList(reqVO);
        int count = tPersonService.cntActor(reqVO);
        dataMap.put("list",list);
        dataMap.put("total",count);
        dataMap.put("page",reqVO.getPage());
        return dataMap;
    }

    @RequestMapping("/addPersonHtml")
    public ModelAndView addPersonHtml(Model model){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/person/addActor");
        model.addAttribute("actor",new TPerson());
        modelAndView.addObject("cList", Arrays.asList(TypeConstants.constellationArr));
        return modelAndView;
    }

    @RequestMapping("/editPersonPage")
    public ModelAndView editPersonPage(@RequestParam("id") long id, Model model){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/person/editActor");
        TPerson tPerson = tPersonService.findPersonWithBLOBsById(id);
        model.addAttribute("actor",tPerson);
        modelAndView.addObject("cList", Arrays.asList(TypeConstants.constellationArr));
        return modelAndView;
    }

    /**
     * 修改人员信息
     * @param tPerson
     * @return
     */
    @RequestMapping("/updatePerson")
    @ResponseBody
    public Map updatePerson(@ModelAttribute("form") TPersonWithBLOBs tPerson) {
        String result = "success";
        try {
            tPersonService.updatePerson(tPerson);
        }catch (Exception e){
            result = "fail";
            e.printStackTrace();
        }
        Map map = new HashMap();
        map.put("result",result);
        return map;
    }

    /**
     * 删除人员
     */
    @RequestMapping("/deletePerson")
    @ResponseBody
    public Map deletePerson(long id) {
        String result = "success";
        try {
            tPersonService.deletePerson(id);
        }catch (Exception e){
            result = "fail";
            e.printStackTrace();
        }
        Map map = new HashMap();
        map.put("result",result);
        return map;
    }

}
